<template>
  <!-- 我的/历史预警 -->
  <div class="bg bg1" ref="content">
    <VSubSection type="1" class="menu" :section="section" :sections="sections" @sectionClick="sectionClick">
    </VSubSection>

    <!-- 刷新 -->
    <list class="list" :loadmoreoffset="50" @loadmore="loadMore" ref="list">
      <!-- 刷新 -->
      <refresh class="refresh" @refresh="refresh(false)" :display="isRefresh ? 'show' : 'hide'">
        <loading-indicator class="load text2"></loading-indicator>
      </refresh>

      <cell>
        <!-- 我的预警 -->
        <VTipPriceCell v-if="menu == 0 && section == 0" v-for="(item, index) in dataList" :key="index" :item="item"
          @func="funcClick($event, item, index)" />

        <VTipIndicatorCell v-if="menu == 0 && (section == 1 || section == 2)" v-for="(item, index) in dataList"
          :key="index" :item="item" @func="funcClick($event, item, index)" />

        <!-- 历史预警 -->
        <VTipRecordCell v-if="menu == 1" v-for="(item, index) in dataList" :key="index" :item="item" />

        <!-- 无更多数据 -->
        <div class="genduo" v-if="pageMax">
          <text class="font22 text3">{{T('没有更多数据')}}</text>
        </div>
      </cell>

    </list>
    <!--弹出菜单-->
    <ACFunc ref="func" @func="funcSelect" />
  </div>

</template>

<style src="@/_css/style.css" ></style>
<style scoped>
  .bg {
    width: 750px;
    padding: 30px 0;
    flex: 1;
    box-sizing: border-box;
  }
  .refresh {
    width: 750px;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .genduo {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 750px;
    height: 100px;
  }
  .wrapper {
    display: flex;
    flex-direction: column;
  }
  .menu {
    margin: 0 30px;
  }
  .load {
    margin-top: 16px;
    height: 40px;
    width: 40px;
  }
</style>

<script>
  //组件
  import VSubSection from "@/_public/VSubSection.vue";
  import VTipIndicatorCell from "@/aide/view/VTipIndicatorCell.vue";
  import VTipPriceCell from "@/aide/view/VTipPriceCell.vue";
  import VTipRecordCell from "@/aide/view/VTipRecordCell.vue";
  import ACFunc from "@/_public/ACFunc";
  import jconfig from "@/_js/config";
  import jmodal from "@/_js/modal";
  import jdebug from "@/_js/debug";
  import jhttps from "@/_js/https";
  import jnavigator from "@/_js/navigator";
  const clipboard = weex.requireModule("clipboard");
  import jbroadcast from "@/_js/broadcast";
  const animation = weex.requireModule("animation"); //动画
  const domModule = weex.requireModule("dom");
  const stream = weex.requireModule("stream"); //网络请求
  const weexFunction = weex.requireModule("WeexFunction");
  import I18N from "@/_i18n/i18n";
  var T = I18N.t;
  weexFunction.setTitle(T("全部提醒"));
  weexFunction.setRightBtn(T("提醒配置"));
  export default {
    components: {
      VSubSection,
      VTipIndicatorCell,
      VTipPriceCell,
      VTipRecordCell,
      ACFunc,
    },
    props: {
      menu: {
        type: Number,
        default: 0,
      },
    },
    data() {
      return {
        Config: jconfig,
        T: T,
        isRefresh: false, //显示刷新
        dataList: [],
        pageNo: 1, //页码
        isRefresh: false, //显示刷新
        pageMax: false, //turn最大页面
        isLoadinging: false, //显示加载
        section: 0,
        sections: [T("价格"), T("指标"), T("形态"), T("Tradingview")],
      };
    },

    created() {
      if (this.menu == 0) {
        this.sections = [T("价格"), T("指标"), T("形态")];
      }
       this.section = jnavigator.getParam("index") || 0;
    },

    mounted() {
      jmodal.showLoad();
      this.refresh(true);
    },

    methods: {
      /********************************** HTTPS*/
      refresh(showLoad = false) {
        if (this.isRefresh || this.isLoadinging) return;
        if (!showLoad) {
          this.isRefresh = true;
        }

        var param = {
          pageNo: 1,
          pageSize: 20,
          indicatorType: this.indicatorType(),
          recordType: this.indicatorType(),
        };
        let url = "indicator/getUserIndicatorList";
        if (this.menu == 1) {
          //历史预警
          url = "indicator/getIndicatorRecordList";
        }
        jhttps.post(url, param, true, (data) => {
          this.isRefresh = false;
          jmodal.hideLoad();
          if (data.status == 200) {
            this.dataList = data.data;
            this.pageNo = 2;
            this.pageMax = data.data.length < 20;
            if (!this.pageMax) {
              this.$refs.list.resetLoadmore();
            }
          } else {
            jmodal.toast(data.msg);
          }
        });
      },

      loadMore() {
        if (this.pageMax || this.isRefresh || this.isLoadinging) return;
        this.isLoadinging = true;

        var param = {
          pageNo: this.pageNo,
          pageSize: 20,
          indicatorType: this.indicatorType(),
          recordType: this.indicatorType(),
        };
        let url = "indicator/getUserIndicatorList";
        if (this.menu == 1) {
          //历史预警
          url = "indicator/getIndicatorRecordList";
        }
        jhttps.post(url, param, true, (data) => {
          this.isLoadinging = false;
          jmodal.hideLoad();
          if (data.status == 200) {
            let dataList = data.data;
            this.dataList.push(...dataList);
            this.pageNo++;
            this.pageMax = data.data.length < 20;
            if (!this.pageMax) {
              this.$refs.list.resetLoadmore();
            }
          } else {
            jmodal.toast(data.msg);
          }
        });
      },

      /********************************** 点击*/
      indicatorType() {
        switch (this.section) {
          case 0:
            return "price";
          case 1:
            return "indicator";
          case 2:
            return "kline";
          case 3:
            return "tradeview";
        }
      },
      sectionClick(index) {
        if (this.section == index) return;
        this.section = index;
        this.dataList = [];
        this.pageMax = false;
        jmodal.showLoad();
        this.refresh(true);
      },
      //功能显示
      funcClick(event, item, index) {
        var funcs = [T("编辑"), T("删除")];
        let isIos = weex.config.env.platform.toUpperCase() != "ANDROID";
        if (isIos) {
          this.$refs.func.show2(funcs, event.position, this.$refs.content, {
            item,
            index,
          });
        } else {
          this.$refs.func.show(funcs, event.position, this.$refs.content, {
            item,
            index,
          });
        }
      },
      //功能选择
      funcSelect(item, index, data) {
        if (item == T("编辑")) {
          
          if (data.item.indicatorType == "indicator") {
            jnavigator.open("", "aide/indicatorAlert", {
              type: data.item.indicatorType,
              indicatorId: data.item.indicatorId,
              id: data.item.id,
              pageRef: "edit",
            });
          } else if (data.item.indicatorType == "price") {
            jnavigator.open("", "aide/priceAlert", {
              type: data.item.indicatorType,
              indicatorId: data.item.indicatorId,
              id: data.item.id,
              pageRef: "edit",
            });
          } else if (data.item.indicatorType == "kline") {
            jnavigator.open("", "aide/kLineAlert", {
              type: data.item.indicatorType,
              indicatorId: data.item.indicatorId,
              id: data.item.id,
              pageRef: "edit",
            });
          }

          jbroadcast.subscribeOnce("IndicatorEdit", () => {
            this.refresh(true);
          });
        }

        if (item == T("删除")) {
          var parm = {
            title: T("提示"),
            message: T("确认删除预警") + " ？",
            okBtn: T("确认"),
            cancelBtn: T("取消"),
          };
          jmodal.confirm(parm, (bool) => {
            if (bool == "true") {
              this.httpsdDeleteById(data.item, data.index);
            }
          });
        }
      },
      //https 删除信号
      httpsdDeleteById(item, index) {
        jmodal.showLoad();
        var param = { id: parseInt(item.id) };
        jhttps.postX("indicator/deleteUserIndicator", param, true, (data) => {
          jmodal.hideLoad();
          if (data.status == 200) {
            this.dataList.splice(index, 1);
            this.dataList = this.dataList.length == 0 ? [] : this.dataList;
            jmodal.toast(T("删除成功"));
          } else {
            jmodal.toast(data.msg);
          }
        });
      },
    },
  };
</script>